package com.itzdm.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itzdm.reggie.common.constant.MessageConstant;
import com.itzdm.reggie.common.exception.LoginFailedException;
import com.itzdm.reggie.dto.EmployeeDTO;
import com.itzdm.reggie.entity.Employee;
import com.itzdm.reggie.mapper.EmployeeMapper;
import com.itzdm.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
    /**
     * 员工登录
     * @param request
     * @param employee
     * @return
     */
    @Override
    public Employee login(HttpServletRequest request, Employee employee) {
        //1.将页面提交的密码进行md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());


        //2.根据页面提交的username查询数据库
        String username = employee.getUsername();
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<Employee>();
        queryWrapper.eq(Employee::getUsername, username);
        //数据库对username进行了唯一处理，只需获得一个即可
        Employee emp = getOne(queryWrapper);

        //3.如果没有查询到则返回登录失败结果
        if (emp == null) {
            throw new LoginFailedException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //4.密码比对，如果不一致返回密码错误提示
        if (!password.equals(emp.getPassword())) {
            throw new LoginFailedException(MessageConstant.PASSWORD_ERROR);
        }

        //5.查看员工状态，如果已禁用，返回已禁用
        if (emp.getStatus() == 0) {
            throw new LoginFailedException(MessageConstant.ACCOUNT_LOCKED);
        }


        //6.登陆成功
        request.getSession().setAttribute("employee", emp.getId());
        return emp;
    }

    /**
     * 新增员工
     * @param request
     * @param employeeDTO
     */
    @Override
    public void saveEmp(HttpServletRequest request,EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        Long id = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(id);
//        employee.setUpdateUser(id);
        this.save(employee);
    }

    /**
     * 分页查询员工
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page pageEmp(int page, int pageSize, String name) {
        //添加分页条件
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //执行查询
        Page p = page(pageInfo, queryWrapper);
        return p;
    }

    /**
     * 启用或禁用员工
     * @param request
     * @param employee
     */
    @Override
    public void updateStatus(HttpServletRequest request, Employee employee) {
//        employee.setUpdateTime(LocalDateTime.now());
        Long id = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateUser(id);
        updateById(employee);
    }
}
